<template>
  <!-- 3 lx品牌管理 -->
  <div class="breadcrumb">
    <Breadcrumb :aaa="aaa" :msgg="msg" :txt="txt" :path1="path1" />
  </div>

  <!-- 主内容 -->
  <div class="brand">
    <!-- 1 -->
    <el-card>
      <div class="brand_top">
        <span>筛选搜索</span>
        <span>
          <el-button type="primary" @click="onSearch">查询结果</el-button>
        </span>
      </div>
      <el-form label-width="100px">
        <el-form-item label="输入搜索">
          <el-input style="width: 200px;" type="text" placeholder="品牌名称/关键字" v-model="name"></el-input>
        </el-form-item>
      </el-form>
    </el-card>

  </div>
  <!-- 2 -->
  <el-card style="margin: 20px">
    <div style="display: flex; justify-content: space-between">
      <div style="margin-top: 10px">数据列表</div>
      <div><el-button @click="addProductCateFn" type="primary">添加</el-button></div>
    </div>
  </el-card>

  <!-- 3 -->
  <div class="dataList" style="margin: 20px">
    <!-- 数据的展示 -->
    <el-table :data="tableData" style="width: 100%" :border="true" @select="onSelect" @select-all="onSelectAll">
      <el-table-column type="selection" fixed prop="id" label="" width="60">
      </el-table-column>
      <el-table-column prop="id" label="编号" width="100" />
      <el-table-column prop="name" label="品牌名称" />
      <el-table-column prop="firstLetter" label="品牌首字母" />
      <el-table-column prop="sort" label="排序" width="180" />
      <el-table-column label="品牌制造商" width="100">
        <template #default="scope">
          <el-switch @click="factoryStatusFn(scope.row.id, scope.row.factoryStatus)"
            :value="scope.row.factoryStatus === 1 ? true : false" />
        </template>
      </el-table-column>
      <el-table-column label="是否显示" width="100">
        <template #default="scope">
          <el-switch props="status" @click="ShowStatusFn(scope.row.id, scope.row.showStatus)"
            :value="scope.row.showStatus === 1 ? true : false" />
        </template>
      </el-table-column>
      <el-table-column prop="adminCount" label="相关">
        <template #default="scope">
          <span>商品:</span><span style="color: blue; margin-left: 5px">100</span>
          <span>商品评论:</span><span style="color: blue; margin-left: 5px">1000</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300" flex="right">
        <template #default="scope">
          <el-button @click="updateProductFn(scope.row.id)">编辑</el-button>
          <el-button type="danger" @click="deltProductFn(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="select-btn">
      <div class="select">
        <el-select v-model="showvalue" class="m-2" placeholder="批量操作">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <el-button type="primary" style="margin-left: 20px" @click="SelectAllFn">确定</el-button>
      </div>
      <!-- 分页器 -->
      <el-pagination class="pagination" v-model:current-page="currentPage" v-model:page-size="pageSize"
        :page-sizes="[5, 10, 15, 20]" :small="small" :disabled="disabled" :background="background"
        layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>
  </div>
</template>

<script setup lang="ts">
import type { Ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { ref } from "vue";
import * as api from "../../request/api";
import { ElMessage, ElMessageBox } from "element-plus";
import Breadcrumb from "../../components/breadcrumb.vue";
let msg = ref("品牌管理");
let txt = ref("商品");
let path1 = ref("pms/addProduct");
let aaa = ref(0);
//路由跳转
const router = useRouter();
//列表数据
let tableData = ref([]) as Ref<any>;
//分页数据
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);
const small = ref(false);
const background = ref(false);
const disabled = ref(false);
// 搜索关键字
let name = ref("");
//进入页面发送请求
let getGoodsBrand = async () => {
  let data = await api.goods.GoodsBrandList({
    keyword: name.value,
    pageNum: currentPage.value,
    pageSize: pageSize.value,
  });
  console.log(data);
  currentPage.value = data.pageNum;
  pageSize.value = data.pageSize;
  total.value = data.total;
  tableData.value = data.list;
};
getGoodsBrand();
let onSearch = async () => {
  // 发送请求,搜索数据
  getGoodsBrand();
};

//显示隐藏商品分类的数据
const showvalue = ref("");
const options = [
  {
    value: 1,
    label: "显示品牌",
  },
  {
    value: 0,
    label: "隐藏品牌",
  },
];
//分页事件
const handleSizeChange = (val: number) => {
  getGoodsBrand();
};
const handleCurrentChange = (val: number) => {
  getGoodsBrand();
};
//添加逻辑
let addProductCateFn = () => {
  router.push({
    path: "/pms/addBrand",
  });
};
//编辑数据
let updateProductFn = (val: number) => {
  router.push({
    path: "/pms/updateBrand",
    // name: 'home',
    query: {
      id: val,
    },
  });
};
//删除逻辑
let deltProductFn = (val: number) => {
  // console.log(row);  // row就是表单数据
  // 二次 确认
  ElMessageBox.confirm("是否删除该品牌", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      console.log(val);

      await api.goods.GoodsDleteBrandList({
        id: val,
      });
      getGoodsBrand();
      ElMessage({
        type: "success",
        message: "操作成功",
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消提交",
      });
    });
};
//开关逻辑实现模块
let factoryStatusFn = async (val: number, val2: number) => {
  if (val2 == 1) {
    val2 = 0;
  } else if (val2 == 0) {
    val2 = 1;
  }
  let newData = await api.goods.updateFactoryStatus({
    ids: val,
    factoryStatus: val2,
  });
  if (newData) {
    ElMessage({
      message: "操作成功",
      grouping: true,
      type: "success",
    });
    getGoodsBrand();
  }
};
let ShowStatusFn = async (val: number, val2: number) => {
  if (val2 == 1) {
    val2 = 0;
  } else if (val2 == 0) {
    val2 = 1;
  }
  let newData = await api.goods.brandupdateshowStatus({
    ids: val,
    showStatus: val2,
  });
  if (newData) {
    ElMessage({
      message: "操作成功",
      grouping: true,
      type: "success",
    });
    getGoodsBrand();
  }
};
//单选全选显示隐藏
let ids = ref([]);
let onSelect = (e: any) => {
  console.log(e);
  let newData = e.map((el: any) => {
    return el.id;
  });
  // console.log(newData);
  ids.value = newData;
  // console.log(ids.value);
};
let onSelectAll = (e: any) => {
  console.log("onSelectAll", e);
  let newData = e.map((el: any) => {
    return el.id;
  });
  // console.log(newData);
  ids.value = newData;
  // console.log(ids.value);
};
//发送请求
let SelectAllFn = async () => {
  if (showvalue.value == null) {
    ElMessage({
      message: "请选取批量操作的类型",
      grouping: true,
      type: "warning",
    });
  } else if (ids.value.length == 0) {
    ElMessage({
      message: "请至少选择一条数据",
      grouping: true,
      type: "warning",
    });
  } else {
    let newData = await api.goods.brandupdateshowStatus({
      ids: ids.value,
      showStatus: showvalue.value,
    });
    if (newData) {
      ElMessage({
        message: "操作成功",
        grouping: true,
        type: "success",
      });
      getGoodsBrand();
    } else {
      ElMessage({
        grouping: true,
        message: "操作失败",
        type: "error",
      });
    }
  }
};
</script>

<style scoped lang="scss">
.select-btn {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
}

.brand {
  /* padding: 20px 15px 20px 15px; */
  padding-top: 30px;
  background-color: #fff;
  margin-bottom: 20px;
  margin: 0 20px;

  .brand_top {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
  }
}



//table表格
:deep .el-table .el-table__cell {
  text-align: center !important;
}

:deep .el-table thead {
  font-weight: bold !important;
}

.btn {
  padding: 20px 15px 20px 15px;
  background-color: #fff;
  margin: 20px;
  display: flex;
  justify-content: space-between;
}

:deep .el-table .el-input {
  width: 100px !important;
}

:deep .el-button {
  font-size: 12px !important;
}

.breadcrumb {
  height: 45px;
  border-bottom: 1px solid #ebeef5;
  font-size: 20px;
}

/* .pagination input {
  width: 100px;
} */
</style>
